package src

import (
	"errors"
	"fmt"
	"gopkg.in/yaml.v2"
	"io/ioutil"
	"os"
)

type Config struct {
	CookieV string
	Webhook string
	Where   []string
	Exclude50WUP bool
}

var DefaultConfig = Config{
	CookieV: "",
	Webhook: "https://open.feishu.cn/open-apis/bot/v2/hook/a2e91422-e680-4b1f-b482-43cd950c72d3",
	Where: []string{
		"量比大于等于1小于等于5",
		"涨跌幅大于3 %",
		"换手率大于3 % 小于10 %",
		"流通市值大于等于50亿小于等于500亿",
		"macd金叉",
		"5日线金叉10日线",
	},
	Exclude50WUP: true,
}

func GetConfig() (*Config,error) {
	filename := "./config.yaml"
	if file, err := os.Open(filename); err == nil {
		defer file.Close()
		if content, err := ioutil.ReadAll(file); err == nil {
			yaml.Unmarshal(content,&DefaultConfig)
			//fmt.Println(DefaultConfig)
			return &DefaultConfig,nil
		} else {
			fmt.Println(err)
			return nil,errors.New("配置文件读取失败")
		}
	} else {
		fmt.Println("正在初始化配置")
		c_,_:=yaml.Marshal(DefaultConfig)
		fmt.Println(string(c_))
		//file, err := os.OpenFile(filename, os.O_CREATE, 0777)
		file, err := os.Create(filename)
		defer file.Close()
		if err == nil {
			if _, err := file.WriteString(string(c_)); err != nil {
				return nil, err
			}
		} else {
			fmt.Println(err)
		}
		return nil,errors.New("请修改配置文件./config.yaml")
	}

}
